<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            padding-top: 50px;
        }

        ul {
            margin-top: 20px;
        }

        ul li {
            margin-top: 10px;
            padding: 10px 0;
            width: 200px;
            border: 1px solid red;
            text-align: center;
        }

        ul li input {
            display: block;
        }

        body>p:nth-of-type(1) {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <input type="checkbox" class="quanxuan">全选
    <ul>

    </ul>
    <p class="num"><span>总数量：</span><span></span></p>
    <p class="price"><span>总价：</span><span></span></p>
    <script>
        let products = [
            {
                goods_name: '小米10-1',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '500',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 10,
                goods_is_checked: true
            },
            {
                goods_name: '小米10-2',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '510',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 50,
                goods_is_checked: false
            }
        ]

        let num = document.querySelector('.num');
        let price = document.querySelector('.price');
        let ul = document.querySelector('ul');
        let quanxuan = document.querySelector('.quanxuan')
        let numText = 0;
        let priceText = 0;

        // 渲染结构
        products.forEach(function (item) {
            ul.innerHTML += `
            <li>
            <input type="checkbox" ${item.goods_is_checked ? 'checked' : ''}>
            <p>${item.goods_name}</p>
            <img src='${item.goods_img}'>
            <p><span>价格：</span><span>${item.goods_price}</span></p>
            <p>${item.goods_attr}</p>
            <p><span>数量：</span><span>${item.goods_num}</span></p>
            <p><span>状态：</span><span>${item.goods_is_checked ? '选中吧' : '没选择中'}<span><p>
            </li>`
            let tag = true;
            if (!item.goods_is_checked) {
                tag = false;
            }
            quanxuan.checked = tag;
            if (item.goods_is_checked) {
                numText += item.goods_num;
                priceText += item.goods_num * item.goods_price;
                num.children[1].innerHTML = numText;
                price.children[1].innerHTML = priceText;
            }
        })

        // 全选
        let ulinp = document.querySelectorAll('ul li input');
        quanxuan.onclick = function () {
            numText = 0;
            priceText = 0;
            for (let i = 0; i < ulinp.length; i++) {
                ulinp[i].checked = this.checked;
                shop(this, i);
            }
        }

        // 当下面的按钮全部选中，全选按扭才选中
        let inp = Array.from(ulinp);
        for (let i = 0; i < ulinp.length; i++) {
            ulinp[i].onclick = function () {
                let tag = inp.every(function (item) {
                    return item.checked;
                })
                quanxuan.checked = tag;
                numText = 0;
                priceText = 0;
                for (let i = 0; i < inp.length; i++) {
                    shop(inp[i], i);
                }
            }
        }

        // 计算价格和判断当前状态
        function shop(obj, a) {
            if (obj.checked) {
                let danjia = inp[a].parentNode.children[3].children[1].innerHTML;
                let shuliang = inp[a].parentNode.children[5].children[1].innerHTML;
                let zhuangtai = inp[a].parentNode.children[6].children[1];
                zhuangtai.innerHTML = '选中吧'
                numText += parseInt(shuliang);
                priceText += parseInt(shuliang * danjia);
            }
            else {
                let zhuangtai = inp[a].parentNode.children[6].children[1];
                zhuangtai.innerHTML = '没选中';
            }
            num.children[1].innerHTML = numText;
            price.children[1].innerHTML = priceText;
        }
    </script>
</body>

</html>